home *** CD-ROM | disk | FTP | other *** search
/ MPEG Toolkit / MPEG Toolkit.iso / dos / mpegstat / decoders.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-01  |  16.9 KB  |  495 lines

  1. /* MPEGSTAT - analyzing tool for MPEG-I video streams
  2.  * 
  3.  * Technical University of Berlin, Germany, Dept. of Computer Science
  4.  * Tom Pfeifer - Multimedia systems project - pfeifer@fokus.gmd.de
  5.  *
  6.  * Jens Brettin, Harald Masche, Alexander Schulze, Dirk Schubert
  7.  *
  8.  * This program uses parts of the source code of the Berkeley MPEG player
  9.  *
  10.  * ---------------------------
  11.  *
  12.  * Copyright (c) 1993 Technical University of Berlin, Germany
  13.  *
  14.  * for the parts of the Berkeley player used:
  15.  *
  16.  * Copyright (c) 1992 The Regents of the University of California.
  17.  * All rights reserved.
  18.  *
  19.  * ---------------------------
  20.  *
  21.  * Permission to use, copy, modify, and distribute this software and its
  22.  * documentation for any purpose, without fee, and without written agreement is
  23.  * hereby granted, provided that the above copyright notices and the following
  24.  * two paragraphs appear in all copies of this software.
  25.  * 
  26.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA 
  27.  * or the Technical University of Berlin BE LIABLE TO ANY PARTY FOR
  28.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  29.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  30.  * CALIFORNIA or the Technical University of Berlin HAS BEEN ADVISED OF THE 
  31.  * POSSIBILITY OF SUCH DAMAGE.
  32.  * 
  33.  * THE UNIVERSITY OF CALIFORNIA and the Technical University of Berlin 
  34.  * SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  35.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  36.  * PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE 
  37.  * UNIVERSITY OF CALIFORNIA and the Technical University of Berlin HAVE NO 
  38.  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, 
  39.  * OR MODIFICATIONS.
  40.  */
  41. /*
  42.  * decoders.h
  43.  *
  44.  * This file contains the declarations of structures required for Huffman
  45.  * decoding
  46.  *
  47.  */
  48.  
  49. /* Include util.h for bit i/o parsing macros. */
  50.  
  51. #include "util.h"
  52.  
  53. /* Code for unbound values in decoding tables */
  54. #define ERROR -1
  55. #define DCT_ERROR 63
  56.  
  57. #define MACRO_BLOCK_STUFFING 34
  58. #define MACRO_BLOCK_ESCAPE 35
  59.  
  60. /* Two types of DCT Coefficients */
  61. #define DCT_COEFF_FIRST 0
  62. #define DCT_COEFF_NEXT 1
  63.  
  64. /* Special values for DCT Coefficients */
  65. #define END_OF_BLOCK 62
  66. #define ESCAPE 61
  67.  
  68. /* Structure for an entry in the decoding table of 
  69.  * macroblock_address_increment */
  70. typedef struct {
  71.   unsigned int value;       /* value for macroblock_address_increment */
  72.   int num_bits;             /* length of the Huffman code */
  73. } mb_addr_inc_entry;
  74.  
  75. /* Decoding table for macroblock_address_increment */
  76. extern mb_addr_inc_entry mb_addr_inc[2048];
  77.  
  78.  
  79. /* Structure for an entry in the decoding table of macroblock_type */
  80. typedef struct {
  81.   unsigned int mb_quant;              /* macroblock_quant */
  82.   unsigned int mb_motion_forward;     /* macroblock_motion_forward */
  83.   unsigned int mb_motion_backward;    /* macroblock_motion_backward */
  84.   unsigned int mb_pattern;            /* macroblock_pattern */
  85.   unsigned int mb_intra;              /* macroblock_intra */
  86.   int num_bits;                       /* length of the Huffman code */
  87. } mb_type_entry;
  88.  
  89. /* Decoding table for macroblock_type in predictive-coded pictures */
  90. extern mb_type_entry mb_type_P[64];
  91.  
  92. /* Decoding table for macroblock_type in bidirectionally-coded pictures */
  93. extern mb_type_entry mb_type_B[64];
  94.  
  95.  
  96. /* Structures for an entry in the decoding table of coded_block_pattern */
  97. typedef struct {
  98.   unsigned int cbp;            /* coded_block_pattern */
  99.   int num_bits;                /* length of the Huffman code */
  100. } coded_block_pattern_entry;
  101.  
  102. /* External declaration of coded block pattern table. */
  103.  
  104. extern coded_block_pattern_entry coded_block_pattern[512];
  105.  
  106.  
  107.  
  108. /* Structure for an entry in the decoding table of motion vectors */
  109. typedef struct {
  110.   int code;              /* value for motion_horizontal_forward_code,
  111.               * motion_vertical_forward_code, 
  112.               * motion_horizontal_backward_code, or
  113.               * motion_vertical_backward_code.
  114.               */
  115.   int num_bits;          /* length of the Huffman code */
  116. } motion_vectors_entry;
  117.  
  118.  
  119. /* Decoding table for motion vectors */
  120. extern motion_vectors_entry motion_vectors[2048];
  121.  
  122.  
  123. /* Structure for an entry in the decoding table of dct_dc_size */
  124. typedef struct {
  125.   unsigned int value;    /* value of dct_dc_size (luminance or chrominance) */
  126.   int num_bits;          /* length of the Huffman code */
  127. } dct_dc_size_entry;
  128.  
  129. /* External declaration of dct dc size lumiance table. */
  130.  
  131. extern dct_dc_size_entry dct_dc_size_luminance[128];
  132.  
  133. /* External declaration of dct dc size chrom table. */
  134.  
  135. extern dct_dc_size_entry dct_dc_size_chrominance[256];
  136.  
  137.  
  138. /* DCT coeff tables. */
  139.  
  140. #define RUN_MASK 0xfc00
  141. #define LEVEL_MASK 0x03f0
  142. #define NUM_MASK 0x000f
  143. #define RUN_SHIFT 10
  144. #define LEVEL_SHIFT 4
  145.  
  146. /* External declaration of dct coeff tables. */
  147.  
  148. extern unsigned short int dct_coeff_tbl_0[256];
  149. extern unsigned short int dct_coeff_tbl_1[16];
  150. extern unsigned short int dct_coeff_tbl_2[4];
  151. extern unsigned short int dct_coeff_tbl_3[4];
  152. extern unsigned short int dct_coeff_next[256];
  153. extern unsigned short int dct_coeff_first[256];
  154.  
  155. #define DecodeDCTDCSizeLum(macro_val)                    \
  156. {                                                    \
  157.   unsigned int index;                                \
  158.                                                      \
  159.   show_bits7(index);                              \
  160.                                                      \
  161.   macro_val = dct_dc_size_luminance[index].value;       \
  162.                                                      \
  163.   flush_bits(dct_dc_size_luminance[index].num_bits); \
  164. }
  165.  
  166. #define DecodeDCTDCSizeChrom(macro_val)                      \
  167. {                                                        \
  168.   unsigned int index;                                    \
  169.                                                          \
  170.   show_bits8(index);                                  \
  171.                                                          \
  172.   macro_val = dct_dc_size_chrominance[index].value;         \
  173.                                                          \
  174.   flush_bits(dct_dc_size_chrominance[index].num_bits);   \
  175. }
  176.  
  177. #define DecodeDCTCoeff(dct_coeff_tbl, run, level)            \
  178. {                                    \
  179.   unsigned int temp, index;                        \
  180.   unsigned int value, next32bits, flushed;                \
  181.                                     \
  182.   /*                                    \
  183.    * Grab the next 32 bits and use it to improve performance of        \
  184.    * getting the bits to parse. Thus, calls are translated as:        \
  185.    *                                    \
  186.    *    show_bitsX  <-->   next32bits >> (32-X)                \
  187.    *    get_bitsX   <-->   val = next32bits >> (32-flushed-X);        \
  188.    *               flushed += X;                \
  189.    *               next32bits &= bitMask[flushed];        \
  190.    *    flush_bitsX <-->   flushed += X;                \
  191.    *               next32bits &= bitMask[flushed];        \
  192.    *                                    \
  193.    * I've streamlined the code a lot, so that we don't have to mask    \
  194.    * out the low order bits and a few of the extra adds are removed.    \
  195.    */                                    \
  196.   show_bits32(next32bits);                        \
  197.                                     \
  198.   /* show_bits8(index); */                        \
  199.   index = next32bits >> 24;                        \
  200.                                     \
  201.   if (index > 3) {                            \
  202.     value = dct_coeff_tbl[index];                    \
  203.     run = value >> RUN_SHIFT;                        \
  204.     if (run != END_OF_BLOCK) {                        \
  205.       /* num_bits = (value & NUM_MASK) + 1; */                \
  206.       /* flush_bits(num_bits); */                    \
  207.       if (run != ESCAPE) {                        \
  208.      /* get_bits1(value); */                    \
  209.      /* if (value) level = -level; */                \
  210.      flushed = (value & NUM_MASK) + 2;                \
  211.          level = (value & LEVEL_MASK) >> LEVEL_SHIFT;            \
  212.      value = next32bits >> (32-flushed);                \
  213.      value &= 0x1;                            \
  214.      if (value) level = -level;                    \
  215.      /* next32bits &= ((~0) >> flushed);  last op before update */    \
  216.        }                                \
  217.        else {    /* run == ESCAPE */                    \
  218.      /* Get the next six into run, and next 8 into temp */        \
  219.          /* get_bits14(temp); */                    \
  220.      flushed = (value & NUM_MASK) + 1;                \
  221.      temp = next32bits >> (18-flushed);                \
  222.      /* Normally, we'd ad 14 to flushed, but I've saved a few    \
  223.       * instr by moving the add below */                \
  224.      temp &= 0x3fff;                        \
  225.      run = temp >> 8;                        \
  226.      temp &= 0xff;                            \
  227.      if (temp == 0) {                        \
  228.             /* get_bits8(level); */                    \
  229.         level = next32bits >> (10-flushed);                \
  230.         level &= 0xff;                        \
  231.         flushed += 22;                        \
  232.          assert(level >= 128);                    \
  233.      } else if (temp != 128) {                    \
  234.         /* Grab sign bit */                        \
  235.         flushed += 14;                        \
  236.         level = ((int) (temp << 24)) >> 24;                \
  237.      } else {                            \
  238.             /* get_bits8(level); */                    \
  239.         level = next32bits >> (10-flushed);                \
  240.         level &= 0xff;                        \
  241.         flushed += 22;                        \
  242.         level = level - 256;                    \
  243.         assert(level <= -128 && level >= -255);            \
  244.      }                                \
  245.        }                                \
  246.        /* Update bitstream... */                    \
  247.        flush_bits(flushed);                        \
  248.        assert (flushed <= 32);                        \
  249.     }                                    \
  250.   }                                    \
  251.   else {                                \
  252.     if (index == 2) {                             \
  253.       /* show_bits10(index); */                        \
  254.       index = next32bits >> 22;                        \
  255.       value = dct_coeff_tbl_2[index & 3];                \
  256.     }                                    \
  257.     else if (index == 3) {                         \
  258.       /* show_bits10(index); */                        \
  259.       index = next32bits >> 22;                        \
  260.       value = dct_coeff_tbl_3[index & 3];                \
  261.     }                                    \
  262.     else if (index) {    /* index == 1 */                \
  263.       /* show_bits12(index); */                        \
  264.       index = next32bits >> 20;                        \
  265.       value = dct_coeff_tbl_1[index & 15];                \
  266.     }                                    \
  267.     else {   /* index == 0 */                        \
  268.       /* show_bits16(index); */                        \
  269.       index = next32bits >> 16;                        \
  270.       value = dct_coeff_tbl_0[index & 255];                \
  271.     }                                    \
  272.     run = value >> RUN_SHIFT;                        \
  273.     level = (value & LEVEL_MASK) >> LEVEL_SHIFT;            \
  274.                                     \
  275.     /*                                    \
  276.      * Fold these operations together to make it fast...        \
  277.      */                                    \
  278.     /* num_bits = (value & NUM_MASK) + 1; */                \
  279.     /* flush_bits(num_bits); */                        \
  280.     /* get_bits1(value); */                        \
  281.     /* if (value) level = -level; */                    \
  282.                                     \
  283.     flushed = (value & NUM_MASK) + 2;                    \
  284.     value = next32bits >> (32-flushed);                    \
  285.     value &= 0x1;                            \
  286.     if (value) level = -level;                        \
  287.                                     \
  288.     /* Update bitstream ... */                        \
  289.     flush_bits(flushed);                        \
  290.     assert (flushed <= 32);                        \
  291.   }                                    \
  292. }
  293.  
  294. #define DecodeDCTCoeffFirst(runval, levelval)         \
  295. {                                                     \
  296.   DecodeDCTCoeff(dct_coeff_first, runval, levelval);  \
  297. }          
  298.  
  299. #define DecodeDCTCoeffNext(runval, levelval)          \
  300. {                                                     \
  301.   DecodeDCTCoeff(dct_coeff_next, runval, levelval);   \
  302. }
  303.  
  304. /*
  305.  *--------------------------------------------------------------
  306.  *
  307.  * DecodeMBAddrInc --
  308.  *
  309.  *      Huffman Decoder for macro_block_address_increment; the location
  310.  *      in which the result will be placed is being passed as argument.
  311.  *      The decoded value is obtained by doing a table lookup on
  312.  *      mb_addr_inc.
  313.  *
  314.  * Results:
  315.  *      The decoded value for macro_block_address_increment or ERROR
  316.  *      for unbound values will be placed in the location specified.
  317.  *
  318.  * Side effects:
  319.  *      Bit stream is irreversibly parsed.
  320.  *
  321.  *--------------------------------------------------------------
  322.  */
  323. #define DecodeMBAddrInc(val)                \
  324. {                            \
  325.     unsigned int index;                    \
  326.     show_bits11(index);                    \
  327.     val = mb_addr_inc[index].value;            \
  328.     flush_bits(mb_addr_inc[index].num_bits);        \
  329. }
  330.  
  331. /*
  332.  *--------------------------------------------------------------
  333.  *
  334.  * DecodeMotionVectors --
  335.  *
  336.  *      Huffman Decoder for the various motion vectors, including
  337.  *      motion_horizontal_forward_code, motion_vertical_forward_code,
  338.  *      motion_horizontal_backward_code, motion_vertical_backward_code.
  339.  *      Location where the decoded result will be placed is being passed
  340.  *      as argument. The decoded values are obtained by doing a table
  341.  *      lookup on motion_vectors.
  342.  *
  343.  * Results:
  344.  *      The decoded value for the motion vector or ERROR for unbound
  345.  *      values will be placed in the location specified.
  346.  *
  347.  * Side effects:
  348.  *      Bit stream is irreversibly parsed.
  349.  *
  350.  *--------------------------------------------------------------
  351.  */
  352.  
  353. #define DecodeMotionVectors(value)            \
  354. {                            \
  355.   unsigned int index;                    \
  356.   show_bits11(index);                    \
  357.   value = motion_vectors[index].code;            \
  358.   flush_bits(motion_vectors[index].num_bits);        \
  359. }
  360. /*
  361.  *--------------------------------------------------------------
  362.  *
  363.  * DecodeMBTypeB --
  364.  *
  365.  *      Huffman Decoder for macro_block_type in bidirectionally-coded
  366.  *      pictures;locations in which the decoded results: macroblock_quant,
  367.  *      macroblock_motion_forward, macro_block_motion_backward,
  368.  *      macroblock_pattern, macro_block_intra, will be placed are
  369.  *      being passed as argument. The decoded values are obtained by
  370.  *      doing a table lookup on mb_type_B.
  371.  *
  372.  * Results:
  373.  *      The various decoded values for macro_block_type in
  374.  *      bidirectionally-coded pictures or ERROR for unbound values will
  375.  *      be placed in the locations specified.
  376.  *
  377.  * Side effects:
  378.  *      Bit stream is irreversibly parsed.
  379.  *
  380.  *--------------------------------------------------------------
  381.  */
  382. #define DecodeMBTypeB(quant, motion_fwd, motion_bwd, pat, intra)    \
  383. {                                    \
  384.   unsigned int index;                            \
  385.                                     \
  386.   show_bits6(index);                            \
  387.                                     \
  388.   quant = mb_type_B[index].mb_quant;                    \
  389.   motion_fwd = mb_type_B[index].mb_motion_forward;            \
  390.   motion_bwd = mb_type_B[index].mb_motion_backward;            \
  391.   pat = mb_type_B[index].mb_pattern;                    \
  392.   intra = mb_type_B[index].mb_intra;                    \
  393.   flush_bits(mb_type_B[index].num_bits);                \
  394. }
  395. /*
  396.  *--------------------------------------------------------------
  397.  *
  398.  * DecodeMBTypeI --
  399.  *
  400.  *      Huffman Decoder for macro_block_type in intra-coded pictures;
  401.  *      locations in which the decoded results: macroblock_quant,
  402.  *      macroblock_motion_forward, macro_block_motion_backward,
  403.  *      macroblock_pattern, macro_block_intra, will be placed are
  404.  *      being passed as argument.
  405.  *
  406.  * Results:
  407.  *      The various decoded values for macro_block_type in intra-coded
  408.  *      pictures or ERROR for unbound values will be placed in the
  409.  *      locations specified.
  410.  *
  411.  * Side effects:
  412.  *      Bit stream is irreversibly parsed.
  413.  *
  414.  *--------------------------------------------------------------
  415.  */
  416. #define DecodeMBTypeI(quant, motion_fwd, motion_bwd, pat, intra)    \
  417. {                                    \
  418.   unsigned int index;                            \
  419.   static int quantTbl[4] = {ERROR, 1, 0, 0};                \
  420.                                     \
  421.   show_bits2(index);                            \
  422.                                     \
  423.   motion_fwd = 0;                            \
  424.   motion_bwd = 0;                            \
  425.   pat = 0;                                \
  426.   intra = 1;                                \
  427.   quant = quantTbl[index];                        \
  428.   if (index) {                                \
  429.     flush_bits (1 + quant);                        \
  430.   }                                    \
  431. }
  432. /*
  433.  *--------------------------------------------------------------
  434.  *
  435.  * DecodeMBTypeP --
  436.  *
  437.  *      Huffman Decoder for macro_block_type in predictive-coded pictures;
  438.  *      locations in which the decoded results: macroblock_quant,
  439.  *      macroblock_motion_forward, macro_block_motion_backward,
  440.  *      macroblock_pattern, macro_block_intra, will be placed are
  441.  *      being passed as argument. The decoded values are obtained by
  442.  *      doing a table lookup on mb_type_P.
  443.  *
  444.  * Results:
  445.  *      The various decoded values for macro_block_type in
  446.  *      predictive-coded pictures or ERROR for unbound values will be
  447.  *      placed in the locations specified.
  448.  *
  449.  * Side effects:
  450.  *      Bit stream is irreversibly parsed.
  451.  *
  452.  *--------------------------------------------------------------
  453.  */
  454. #define DecodeMBTypeP(quant, motion_fwd, motion_bwd, pat, intra)    \
  455. {                                    \
  456.   unsigned int index;                            \
  457.                                     \
  458.   show_bits6(index);                            \
  459.                                     \
  460.   quant = mb_type_P[index].mb_quant;                    \
  461.   motion_fwd = mb_type_P[index].mb_motion_forward;            \
  462.   motion_bwd = mb_type_P[index].mb_motion_backward;            \
  463.   pat = mb_type_P[index].mb_pattern;                    \
  464.   intra = mb_type_P[index].mb_intra;                    \
  465.                                     \
  466.   flush_bits(mb_type_P[index].num_bits);                \
  467. }
  468. /*
  469.  *--------------------------------------------------------------
  470.  *
  471.  * DecodeCBP --
  472.  *
  473.  *      Huffman Decoder for coded_block_pattern; location in which the
  474.  *      decoded result will be placed is being passed as argument. The
  475.  *      decoded values are obtained by doing a table lookup on
  476.  *      coded_block_pattern.
  477.  *
  478.  * Results:
  479.  *      The decoded value for coded_block_pattern or ERROR for unbound
  480.  *      values will be placed in the location specified.
  481.  *
  482.  * Side effects:
  483.  *      Bit stream is irreversibly parsed.
  484.  *
  485.  *--------------------------------------------------------------
  486.  */
  487. #define DecodeCBP(coded_bp)                        \
  488. {                                    \
  489.   unsigned int index;                            \
  490.                                     \
  491.   show_bits9(index);                            \
  492.   coded_bp = coded_block_pattern[index].cbp;                \
  493.   flush_bits(coded_block_pattern[index].num_bits);            \
  494. }
  495.